--require "MainScene"

XunLianYingScene={}
XunLianYingSceneFunc={}
XunLianYingSceneFunc.__index = XunLianYingSceneFunc
setmetatable(XunLianYingScene, XunLianYingSceneFunc)

XunLianYingScene.NodeID = {TabelBK=1010, UpButton=1020}
local AssignSoldierItemID = {TimeLabel=2001, CashLabel=2002, NumLabel=2003, Button=2004}
--XunLianYingScene.upgradeCooldown = 0
XunLianYingScene.assignCooldown = 0

XunLianYingSceneFunc.getNode = function()
  local l = {}
  setmetatable(l, XunLianYingSceneFunc)
  l.node = CCBReader:load("XunLianYingScene.ccbi")
  print("XunLianYingScene.getNode", XunLianYingScene.node)
  XunLianYingScene.instance = l
  l:init() 
  return l 
end

XunLianYingSceneFunc.getScene = function()
  local sc = CCScene:create()
  sc:addChild(XunLianYingScene.getNode().node)  
  print("XunLianYingScene.getScene", sc)
  XunLianYingScene.scene = sc
  return sc
end

XunLianYingSceneFunc.init = function(self)
  local b = self.node:getChildByTag(XunLianYingScene.NodeID.UpButton)
  setObjectExtTable(b, self)
  b = self.node:getChildByTag(1111)
  setObjectExtTable(b, self)
  self:initTable()

  self.upBuildingButton = TypeCast(self.node:getChildByTag(1020), "CCButton")
  self.timeLabel = TypeCast(self.node:getChildByTag(1006), "CCLabelTTF")

  --剩余冷却时间
  --XunLianYingScene.assignCooldown = TimeManager.getLeftDuration("assigncd") 
 
  setBuildingBaseData(BuildingID.XunLianYing, self.node)
  --self:setUpgradeCoolDown()   
  --self:setAssignCoolDown()   
  --UpgradeManager.checkUpgrade(BuildingID.XunLianYing)

  local tmp = UITimer.getUITimer(string.format("upbuildingcd_%d", BuildingID.XunLianYing), self, ccp(160,72), ccp(0, 0))
  if tmp~=nil then
    tmp.view:setPosition(ccp(710, 420))
    self.node:addChild(tmp.view)
    self.upBuildingTimer = tmp
    self.timeLabel:setVisible(false)
    self.upBuildingButton:setVisible(false)
    self.upBuildingButton:setEnabled(false)
  end
  tmp = UITimer.getUITimer("assigncd", self, ccp(-15,15), ccp(0, 0))
  if tmp~=nil then
    tmp.view:setPosition(ccp(725, 40))
    self.node:addChild(tmp.view)
    self.assignTimer = tmp
    self:setEnabledAssignButton(false)
  end
  XunLianYingGuide.nextStep()
end

XunLianYingSceneFunc.initTable = function(self)
  --self.unlockedItems 
  local items = CCArray:create()
  self.unlockedItems = Array:create() 
  self.buttons = {}
  --local begin = 18
  for i=1,6  do
    local item = CCBReader:load("AssignSoldierItem.ccbi")
    local info = UserData.config.models.SoldierRecruitContract[i] 
    local lb1 = item:getChildByTag(AssignSoldierItemID.TimeLabel)
    TypeCast(lb1, "CCLabelTTF"):setString(formatSecond(info.duration))
    local lb2 = item:getChildByTag(AssignSoldierItemID.CashLabel)
    TypeCast(lb2, "CCLabelTTF"):setString(string.format("%d", info.cash))
    
    local buildingLevel = UserData.userinfo.host.cities2[tostring(BuildingID.XunLianYing)].l
    local num = math.ceil(info.num*UserData.config.models.CampTrainLevel[buildingLevel].getSoldierAdd) 
    --return Math.ceil(num * CampTrainLevel(UserProfileManager.host.newCity.getBuildingById(WarConstants.BUILDING2_TRAIN).buildingLevel).getSoldierAdd);
    local lb3 = item:getChildByTag(AssignSoldierItemID.NumLabel)
    TypeCast(lb3, "CCLabelTTF"):setString(string.format("%d", num))
    print("XunLianYingScene.initTable", info.duration, info.cash, info.num)
    local btn = item:getChildByTag(AssignSoldierItemID.Button)
    btn:setTag(i) 
    setObjectExtTable(btn, self)
    self.buttons[#self.buttons+1] = btn

    print("XunLianYingScene.initTable", info.duration, info.cash, info.num)
    local level = UserData.userinfo.host.cities2[tostring(BuildingID.XunLianYing)].l
    if info.level > level then
      local sp = CCSprite:create("mask2.png")
      local lb = CCLabelTTF:create(string.format("需要等级%d", info.level), "Helvetica", 28)
      sp:setPosition(ccp(160,95))
      lb:setPosition(ccp(160,50))
      item:addChild(sp)
      item:addChild(lb)
      setTouchEnabledAll(item, false)
    else
      self.unlockedItems:append(item)
    end
    item:setContentSize(CCSizeMake(320, 190))
    --items1:addObject(item)
    items:addObject(item)
  end
  --print("XunLianYingSceneFunc.setEnabledAssignButton", self.unlockedItems:count())
  --local tableBK = self.node:getChildByTag(XunLianYingScene.NodeID.TabelBK)
  --local size = tableBK:getContentSize()
  local tableview = CCTableView:create(0, CCSizeMake(850, 190), items)
  --local x, y = ActionUtil.leftBottomPoint(tableBK)
  tableview:setPosition(ccp(55, 125))
  self.node:addChild(tableview)
  self.assignItems = items
end

XunLianYingSceneFunc.setEnabledAssignButton = function(self, enable)
  for i=0,self.unlockedItems:size()-1 do
    local item = self.unlockedItems:at(i)
    setTouchEnabledAll(item, enable) 
  end
end

XunLianYingSceneFunc.showSoldierCount = function(self, count)
  showMessage(string.format("兵力增加%d", count), ccc3(0,255,0))
end

--[[
XunLianYingSceneFunc.setUpgradeCoolDown = function(self)
  local lb = self.node:getChildByTag(1006)
  local b = self.node:getChildByTag(XunLianYingScene.NodeID.UpButton)
  if XunLianYingScene.upgradeCooldown==0 then
    TypeCast(lb, "CCLabelTTF"):setString("等待升级")
    TypeCast(b, "CCButton"):setEnabled(true)
  else
    TypeCast(lb, "CCLabelTTF"):setString(formatSecond(XunLianYingScene.upgradeCooldown))
    TypeCast(b, "CCButton"):setEnabled(false)
  end
end]]
--[[
XunLianYingSceneFunc.setAssignCoolDown = function(self)
  local lb = self.node:getChildByTag(1007)
  if XunLianYingScene.assignCooldown<=0 then
    TypeCast(lb, "CCLabelTTF"):setString("等待招募")
    self:setEnabledAssignButton(true)
    print("XunLianYingSceneFunc.setEnabledAssignButton", self.unlockedItems:size())
    --local b = self.node:getChildByTag(XunLianYingScene.NodeID.UpButton)
    --TypeCast(b, "CCButton"):setEnabled(true)
  else
    if XunLianYingScene.assignTimerID==nil then
      XunLianYingScene.assignTimerID = CCDirector:sharedDirector():getScheduler():scheduleScriptFunc(onAssignTimer, 1, false)
    end
    self:setEnabledAssignButton(false)
    TypeCast(lb, "CCLabelTTF"):setString(formatSecond(XunLianYingScene.assignCooldown))
  end
end
]]

XunLianYingSceneFunc.onUpgradeButton = function(self, node, event)
  if XunLianYingScene.upgradeCooldown==nil or XunLianYingScene.upgradeCooldown<=0 then
    --setTouchEnabledAll(self.node, false)
    LoadScene:showLoadingScene()
    NetworkRequest.upgradeBuilding(BuildingID.XunLianYing, "onUpgradeXunLianYingComplete", self)
    --NetworkRequest.test("onUpgradeXunLianYingComplete", self)
  else
    --JiaSuDialog:create(BuildingID.XunLianYing, self)
  end
end

XunLianYingSceneFunc.onUpgradeXunLianYingComplete = function(self, data)
  local res = cjson.decode(data)
  if res.ret==0 then
  --if true then
    NetworkRequest.syncData(res)
    showMessage("升级成功", ccc3(0,255,0))
    --UpgradeManager.upgradeBuilding(BuildingID.XunLianYing)
    local bl = UserData.userinfo.host.cities2[tostring(BuildingID.XunLianYing)].l
    local binfo = UserData.config.models[BuildingLevel[BuildingID.XunLianYing]][bl]
    UserData.userinfo.host.cities2[tostring(BuildingID.XunLianYing)].l = bl+1
    --setBuildingBaseData(BuildingID.XunLianYing, self.node)

    self.upBuildingTimer = UITimer.createUITimer(string.format("upbuildingcd_%d", BuildingID.XunLianYing), binfo.coolingTime, self, ccp(160,72), ccp(0, 0))
    self.upBuildingTimer.view:setPosition(ccp(710, 420))
    self.node:addChild(self.upBuildingTimer.view)
    self.timeLabel:setVisible(false)
    self.upBuildingButton:setVisible(false)
  else
    showMessage(res.msg, ccc3(255,255,0))
    --showMessage("error", ccc3(255,255,0))
    local b = self.node:getChildByTag(XunLianYingScene.NodeID.UpButton)
    TypeCast(b, "CCButton"):setEnabled(true)
  end
  --setTouchEnabledAll(self.node, true)
  LoadScene:removeLoadingScene()
  self:closeScene()
  MainScene.scene:addChild(XunLianYingScene:getNode().node)
end

XunLianYingSceneFunc.onAssignSoldierButton = function(self, node)
  local bl = UserData.userinfo.host.cities2[tostring(BuildingID.JunYing)].l
  local binfo = UserData.config.models[BuildingLevel[BuildingID.JunYing]][bl]
  if UserData.userinfo.host.soldiers >= binfo.soldierMax then
    showMessage("已达兵力上限", ccc3(255,0,0))
    return
  end 
  --self:setEnabledAssignButton(false)
  local tag = node:getTag()
  self.assignID = tag
  NetworkRequest.assignSoldier(tag, "onAssignSoldierComplete", self)  
  LoadScene:showLoadingScene()
  --setTouchEnabledAll(self.node, false)
  --XunLianYingScene.showSoldierCount(300)
end
XunLianYingSceneFunc.onAssignSoldierComplete = function(self, data)
  local res = cjson.decode(data)
  if res.ret==0 then
    NetworkRequest.syncData(res)
    local info = UserData.config.models.SoldierRecruitContract[self.assignID]
    showMessage("success", ccc3(0,255,0))
  
    local tmp = UITimer.createUITimer("assigncd", info.duration, self, ccp(-15,15), ccp(0, 0))
    tmp.view:setPosition(ccp(725, 40))
    self.node:addChild(tmp.view)
    self.assignTimer = tmp
  else
    showMessage(res.msg, ccc3(255,255,0))
  end
  LoadScene:removeLoadingScene()
  self:closeScene()
  MainScene.scene:addChild(XunLianYingScene:getNode().node)
  --XunLianYingGuide.nextStep()
end
---------------------------------------------------------------------------------------------------------------
--处理加速
XunLianYingSceneFunc.onTimerEnd = function(self, timer)
  if timer~=nil then
    timer.view:getParent():removeChild(timer.view, true)
  end
  if timer==self.assignTimer then
    self:setEnabledAssignButton(true)
  else
    self.timeLabel:setVisible(true)
    self.upBuildingButton:setVisible(true)
    self.upBuildingButton:setEnabled(true)
  end
end

XunLianYingSceneFunc.onJiaSuButton = function(self, timer, pars)
  local tmp = JiaSuSampleDialog:create(pars.leftTime, self)
  MainScene.scene:addChild(tmp.view)
  UITimer.removeUITimer(self.assignTimer)
  UITimer.removeUITimer(self.upBuildingTimer)
  MainScene.scene:removeChild(self.node, true)
  if timer==self.assignTimer then
    self.jiasuAssignDlg = tmp
  elseif timer==self.upBuildingTimer then
    self.jiasuUpBuildingDlg = tmp
  end
end

XunLianYingSceneFunc.onJiaSuConfirm = function(self, dlg, confirm, timereel, timeLeft)
  if confirm~=true then
    MainScene.scene:removeChild(dlg.view, true)
    MainScene.scene:addChild(XunLianYingScene.getNode().node)
  elseif dlg==self.jiasuUpBuildingDlg then
    --NetworkRequest.test("onJiaSuComplete", self)
    NetworkRequest.accelerate("Upgrading", BuildingID.XunLianYing, timereel, "onJiaSuComplete", self)
    --setTouchEnabledAll(dlg.view, false)
    LoadScene:showLoadingScene()
  elseif dlg==self.jiasuAssignDlg then
    NetworkRequest.accelerate("Product", BuildingID.XunLianYing, timereel, "onJiaSuComplete", self)
    --setTouchEnabledAll(dlg.view, false)
    LoadScene:showLoadingScene() 
  end
end
XunLianYingSceneFunc.onJiaSuComplete = function(self, data)
  LoadScene:removeLoadingScene()
  local res = cjson.decode(data)
  if res.ret==0 then
  --if true then
    if self.jiasuAssignDlg~=nil then
      self.assignTimer:endTimer()
    end
    if self.jiasuUpBuildingDlg~=nil then
      self.upBuildingTimer:endTimer()
    end
  else
    showMessage(res.msg, ccc3(255,255,0))
  end
  
  if self.jiasuUpBuildingDlg~=nil then
    MainScene.scene:removeChild(self.jiasuUpBuildingDlg.view, true)
  end
  if self.jiasuAssignDlg~=nil then
    MainScene.scene:removeChild(self.jiasuAssignDlg.view, true)
  end
  MainScene.scene:addChild(XunLianYingScene.getNode().node)
end
--------------------
XunLianYingSceneFunc.closeScene = function(self)
  UITimer.removeUITimer(self.assignTimer)
  UITimer.removeUITimer(self.upBuildingTimer)
  MainScene.scene:removeChild(self.node, true)
  XunLianYingScene.instance = nil
end
XunLianYingSceneFunc.onClose = function(self, node, event)
  self:closeScene() 
  setTouchEnabledAll(MainScene.node, true)
  MainScene.setState()
end
